﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Threading;
using WLYT.Application.Interfaces;
using WLYT.Application.Services;
using WLYT.Domain.Entries;
using WLYT.Domain.Enum;
using WLYT.Domain.Model;

namespace WLYT.WMS.Controllers
{
    [ApiController]
    [Authorize]
    public class OrderController(IBaseService<Order> orderService, IBaseService<OrderItem> orderItemService) : ControllerBase
    {
        /// <summary>
        /// 出库单
        /// </summary>
        /// <param name="type"></param>
        /// <param name="priority"></param>
        /// <param name="status"></param>
        /// <param name="orderNo"></param>
        /// <param name="line"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/OutOrder/GetData")]
        public async Task<Table<Order>> GetData(int? type = null,
                                                int? priority = null,
                                                int? status = null,
                                                string orderNo = "",
                                                string line = "",
                                                DateTime? startDate = null,
                                                DateTime? endDate = null,
                                                int pageSize = 20,
                                                int pageIndex = 1)
        {
            Expressionable<Order> exp = Expressionable.Create<Order>();
            exp.AndIF(type != null, p => p.Type == type);
            exp.AndIF(priority != null, p => p.Priority == priority);
            exp.AndIF(!string.IsNullOrEmpty(orderNo), p => p.OrderNo.Contains(orderNo));
            exp.AndIF(!string.IsNullOrEmpty(line), p => p.Line == line);
            if (status != null)
            {
                if (status == (int)OrderStatus.出库中)
                {
                    exp.And(p => p.Status == (int)OrderStatus.出库中 || p.Status == (int)OrderStatus.部分运输开始);
                }
                else
                {
                    exp.And(p => p.Status == status);
                }
            }
            exp.AndIF(startDate != null, p => p.CreateTime >= startDate);
            exp.AndIF(endDate != null, p => p.CreateTime <= endDate);
            var result = await orderService.GetPageListAsync(exp, pageSize, pageIndex);
            return new Table<Order>(result.Item1, result.Item2);
        }


        /// <summary>
        /// 出库单--详情
        /// </summary>
        /// <param name="reelId"></param>
        /// <param name="originalCode"></param>
        /// <param name="warehouseNo"></param>
        /// <param name="orderNo"></param>
        /// <param name="boxCode"></param>
        /// <param name="status"></param>
        /// <param name="isUpload"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/OrderItem/GetData")]
        public async Task<Table<OrderItem>> GetItemData(string reelId = "",
                                                    string originalCode = "",
                                                    string warehouseNo = "",
                                                    string orderNo = "",
                                                    string boxCode = "",
                                                    int? status = null,
                                                    bool? isUpload = null,
                                                    DateTime? startDate = null,
                                                    DateTime? endDate = null,
                                                    int pageSize = 20,
                                                    int pageIndex = 1)
        {
            Expressionable<OrderItem> exp = Expressionable.Create<OrderItem>();
            exp.AndIF(status != null, p => p.Status == status);
            exp.AndIF(!string.IsNullOrEmpty(reelId), p => p.ReelId.Contains(reelId));
            exp.AndIF(!string.IsNullOrEmpty(originalCode), p => p.OriginalCode.Contains(originalCode));
            exp.AndIF(!string.IsNullOrEmpty(warehouseNo), p => p.WarehouseNo == warehouseNo);
            exp.AndIF(!string.IsNullOrEmpty(orderNo), p => p.OrderNo == orderNo);
            exp.AndIF(!string.IsNullOrEmpty(boxCode), p => p.BoxCode == boxCode);
            exp.AndIF(isUpload != null, p => p.IsUpLoad == isUpload);
            exp.AndIF(startDate != null, p => p.InOrOutTime >= startDate);
            exp.AndIF(endDate != null, p => p.InOrOutTime <= endDate);
            var result = await orderItemService.GetPageListAsync(exp, pageSize, pageIndex); 
            return new Table<OrderItem>(result.Item1, result.Item2);
        }
    }
}
